package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.g2room.driving.util.DbUtil;
import com.g2room.driving.util.IpUtil;

public class GenericMyuall {
	static Map<String, Map<String, String>> cookies = new HashMap<String, Map<String, String>>();
	public Document getDoc(String url) throws IOException {
		String ip = IpUtil.getIp();
		Document doc =Jsoup.connect(url)
//				  .data("query", "Java")   // 请求参数
				.header("X-Real-IP", ip)
				.header("x-forwarded-for", ip)
				.header("WL-Proxy-Client-IP", ip)
				.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2") // 设置 User-Agent 
//				.cookie("auth", "token") // 设置 cookie 
				.timeout(5000)           // 设置连接超时时间
				.get(); // get方式访问
		return doc;
	}
	
	public String getJsonContent(String url) throws IOException {
		String ip = IpUtil.getIp();
		return Jsoup.connect(url).timeout(60000).header("X-Real-IP", ip)
				.header("x-forwarded-for", ip)
				.header("WL-Proxy-Client-IP", ip)
				.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2") // 设置 User-Agent
				.ignoreContentType(true).execute().body();
	}
	
	public String getJsonContentByCookie(String url, String start) throws IOException {
		String ip = IpUtil.getIp();
		Map<String, String> cookie = cookies.get(url);
		if(null == cookie) {
			Response resp = Jsoup.connect(url).timeout(60000).header("X-Real-IP", ip)
					.header("x-forwarded-for", ip)
					.header("WL-Proxy-Client-IP", ip)
					.header("Host", "www.gaokaopai.com")
					.header("Upgrade-Insecure-Requests", "1")
					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2") // 设置 User-Agent
					.ignoreContentType(true).execute();
			cookie = resp.cookies();
			cookies.put(url, cookie);
		}
		Document doc = Jsoup.connect(url).timeout(60000).header("X-Real-IP", ip)
				.header("x-forwarded-for", ip)
				.header("WL-Proxy-Client-IP", ip)
				.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
				.header("Host", "www.gaokaopai.com")
				.header("X-Requested-With", "XMLHttpRequest")
				.header("Referer", "http://www.gaokaopai.com/rank-index.html")
				.header("Upgrade-Insecure-Requests", "1")
				.header("domain", "http://www.gaokaopai.com")
				.header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
				.header("Accept-Encoding", "gzip, deflate")
				.cookies(cookie)
				// ?amount=25&city=&otype=4&start=25
				.data("amount", "25")
				.data("city", "")
				.data("otype", "4")
				.data("start", start)
				.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2") // 设置 User-Agent
				.ignoreContentType(true).post();
		
		return doc.body().text();
	}
	
	public static void main(String[] args) {
		String url = "http://data.api.gkcx.eol.cn/soudaxue/querySpecialtyScore.html?messtype=json&url_sign=querySpecialtyScore&size=50&page=1569";
		System.out.println(3146253 / 50);
	}
	
	public static String handleSpace(String str) {
		return str.replaceAll("&nbsp;", " ");
	}
	
	public static List<String> getTitles() {
		String sql = "select a.title from ncee_acticle a";
		List<String> articles = new ArrayList<String>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				articles.add(rs.getString("title"));
			}
			rs.close();
			statement.close();
//			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
}
